package com.bls.productmall.controller;

import com.bls.productmall.entity.Product;
import com.bls.productmall.service.ProductService;
import com.bls.productmall.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //开放前端的跨域访问
@RestController
@RequestMapping("/api/product")
@Slf4j
public class ProductController {

    @Resource
    private ProductService productService;

    @GetMapping("/getProductList/{current}/{size}")
    public R getProductList(@PathVariable int current, @PathVariable int size){
        log.info("分页获取产品列表...");
        return productService.getProductList(current, size);
    }

    @GetMapping("/getProductByTitle/{title}/{current}/{size}")
    public R getProductByTitle(@PathVariable String title,@PathVariable int current, @PathVariable int size){
        log.info("根据产品名搜索产品...");
        return productService.getProductByTitle(title, current, size);
    }

    @PostMapping("/addProduct")
    public R addProduct(@RequestBody Product product){
        log.info("增加产品...");
        int i = productService.addProduct(product);
        return R.ok().setMessage("成功添加" + i + "个产品");
    }

    @DeleteMapping("/deleteProduct/{productId}")
    public R deleteProduct(@PathVariable int productId){
        log.info("删除产品...");
        int i = productService.deleteProduct(productId);
        return R.ok().setMessage("成功删除" + i + "个产品");
    }

    @PostMapping("/updateProduct")
    public R updateProduct(@RequestBody Product product){
        log.info("更新产品...");
        int i = productService.updateProduct(product);
        return R.ok().setMessage("成功更新" + i + "个产品");
    }
}

